package pnu.ssm.sbmother;

import pnu.ssm.http.HttpConnectorParent;
import pnu.ssm.key.Key;
import pnu.ssm.key.SBString;
import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

@SuppressLint("ValidFragment")
public class SituationTab extends Fragment implements Button.OnClickListener {

	public static final String TAG = "SituationTab";
	private static final int SITUATION_UPDATE = 42;

	HttpConnectorParent httpConnector;
	Context mContext;
	Button btn_situation_update;
	ProgressDialog progressDialog;
	SharedPreferences pref;
	SharedPreferences.Editor editor;
	
	TextView tv_situ_curr_level;
	TextView tv_situ_curr_obtain;
	TextView tv_situ_curr_use_time;
	
	Handler situHandler = new Handler(){
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case SITUATION_UPDATE:
				Bundle bundle = msg.getData();
				String rating = bundle.getString("rating");
				String obtain_time = bundle.getString("obtain_time");
				String spare_time = bundle.getString("spare_time");
				
				tv_situ_curr_level.setText(rating+"급");
				tv_situ_curr_obtain.setText(obtain_time+"분");
				tv_situ_curr_use_time.setText(spare_time+"분");
				break;

			default:
				break;
			}
		};
	};

	public SituationTab(Context context) {
		mContext = context;
		httpConnector = HttpConnectorParent.getInstance();
		pref = mContext.getSharedPreferences(Key.prefKey, Context.MODE_PRIVATE);
		editor = pref.edit();
		progressDialog = new ProgressDialog(mContext);
		progressDialog.setMessage(SBString.WAIT);
		progressDialog.setIndeterminate(true);
		progressDialog.setCancelable(true);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.tab_situation, null);

		btn_situation_update = (Button) view.findViewById(R.id.btn_situation_update);
		btn_situation_update.setOnClickListener(this);

		tv_situ_curr_level = (TextView) view.findViewById(R.id.tv_situ_curr_level);
		tv_situ_curr_obtain = (TextView) view.findViewById(R.id.tv_situ_curr_obtain);
		tv_situ_curr_use_time = (TextView) view.findViewById(R.id.tv_situ_curr_use_time);
		return view;
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.btn_situation_update:
			new AsyncTask<Void, Void, Void>(){
				protected void onPreExecute() {
					progressDialog.show();
				};
				@Override
				protected Void doInBackground(Void... params) {
					String child_num = pref.getString(Key.child_num, "");
					String[] result = httpConnector.getChineseSetting(child_num);
					String extra_time = result[0];
					String limit_time = result[1];
					String rating = result[2];
					String obtain_time = result[3];
					String spare_time = result[4];
					
					Message msg = Message.obtain();
					msg.what = SITUATION_UPDATE;
					Bundle bundle = new Bundle();
					bundle.putString("rating", rating);
					bundle.putString("obtain_time", obtain_time);
					bundle.putString("spare_time", spare_time);
					msg.setData(bundle);
					
					situHandler.sendMessage(msg);
					return null;
				}
				protected void onPostExecute(Void result) {
					progressDialog.dismiss();
				};
			}.execute(null,null,null);
			break;
		default:
			Log.e(TAG, "default error");
			break;
		}
	}

}